package com.ctshk.rpc.air.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.air.entity.AirProductCost;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 机票产品成本表 Mapper 接口
 * </p>
 *
 * @author lihang
 * @since 2021-03-05
 */
public interface AirProductCostMapper extends BaseMapper<AirProductCost> {


    /**
     * 連表查詢機票成本  table t_air_product_cost  t_air_product_cost_date
     * @param queryWrapper
     * @return
     */
    @Select("SELECT c.* FROM t_air_product_cost c LEFT JOIN t_air_product_cost_date cd ON c.id = cd.cost_id"+
            " ${ew.customSqlSegment}")
    List<AirProductCost> queryCost(@Param("ew") QueryWrapper queryWrapper);

    /**
     *  获取有效的成本
     * @param productId
     * @param date
     * @return
     */
    @Select("SELECT \n" +
            "  apc.* \n" +
            "FROM\n" +
            "  `t_air_product_cost` apc \n" +
            "  LEFT JOIN `t_air_product_cost_date` apcd \n" +
            "    ON apcd.`cost_id` = apc.`id` \n" +
            "WHERE apc.`air_product_id` = #{productId} \n" +
            "  AND #{date} BETWEEN apcd.date_begin \n" +
            "  AND apcd.date_end ORDER BY apcd.date_begin ASC")
    List<AirProductCost> selectValidDateCost(@Param("productId") Long productId,@Param("date") LocalDate date);

    /**
     * 查询当前有效的成本
     * limit1是避免同一个时间段出现多个成本导致报错
     * @param productId
     * @param date
     * @return
     */
    @Select("SELECT \n" +
            "  apc.* \n" +
            "FROM\n" +
            "  `t_air_product_cost` apc \n" +
            "  LEFT JOIN `t_air_product_cost_date` apcd \n" +
            "    ON apcd.`cost_id` = apc.`id` \n" +
            "WHERE apc.`air_product_id` = #{productId} \n" +
            "  AND #{date} BETWEEN apcd.`date_begin` \n" +
            "  AND apcd.`date_end` \n" +
            "  ORDER BY gmt_create ASC\n" +
            "  LIMIT 1")
    AirProductCost getCurrentlyValidCost(@Param("productId") Long productId,@Param("date") LocalDate date);
}
